package Student;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//维护表格
public class JTableStudents extends JFrame{


    private DefaultTableModel tableModel;   //表格模型对象
    private JTable table;

    JTextField id_textfiled = new JTextField(10);
    JTextField name_textfiled = new JTextField(10);

    public JTableStudents(){
        setSize(750, 400);
        // 获取屏幕的尺寸
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        // 设置程序窗口的位置为屏幕的正中央
        this.setLocation((dimension.width-getWidth())/2, (dimension.height-getHeight())/2);
        setTitle("学生信息列表");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel p1 = new JPanel();
        p1.add(new JLabel("请输入学生学号："));
        p1.add(id_textfiled);
        p1.add(new JLabel("请输入学生姓名："));
        p1.add(name_textfiled);
        JButton query_button = new JButton("查询—>");
        query_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String no = null;
                if(id_textfiled.getText()!=null && !(id_textfiled.getText().trim().isEmpty())){
                    no = id_textfiled.getText().trim();
                }
                String name=null;
                if(name_textfiled.getText()!=null &&!(name_textfiled.getText().trim().isEmpty())){
                    name = name_textfiled.getText().trim();
                }
                tableModel.setDataVector(Dao.getInstance().queryByNoAndName(no, name), Dao.getInstance().getColumnNames());
            }
        });

        JButton reset_button = new JButton("清除");
        reset_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                id_textfiled.setText("");
                name_textfiled.setText("");
//				profession_textfiled.setText("");
                tableModel.setRowCount(0);

            }
        });

        JButton all_button = new JButton("显示所有记录");
        all_button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tableModel.setDataVector(Dao.getInstance().getDataByVector(), Dao.getInstance().getColumnNames());
            }
        });

        p1.add(query_button);
        p1.add(reset_button);
        p1.add(all_button);
        getContentPane().add(p1,BorderLayout.NORTH);

        tableModel = new DefaultTableModel(Dao.getInstance().getDataByVector(),Dao.getInstance().getColumnNames());
        table = new JTable(tableModel){	// 单元格可编辑
            public boolean isCellEditable(int row, int column) {return false;};
        };

        JScrollPane scrollPane = new JScrollPane(table);   //支持滚动
        getContentPane().add(scrollPane,BorderLayout.CENTER);
        //排序:
        table.setRowSorter(new TableRowSorter<DefaultTableModel>(tableModel));
        table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  //单选

        scrollPane.setViewportView(table);
        final JPanel panel = new JPanel();
        getContentPane().add(panel,BorderLayout.SOUTH);
        JButton addButton = new JButton("添加");   //添加按钮
        addButton.addActionListener(new ActionListener(){//添加事件
            public void actionPerformed(ActionEvent e){
                DetailStudent dinfo = new DetailStudent();
                dinfo.addWindowListener(new WindowAdapter() {
                    public void windowClosed(WindowEvent e) {
                        tableModel.setDataVector(Dao.getInstance().getDataByVector(), Dao.getInstance().getColumnNames());
                    }
                });
                dinfo.setVisible(true);
            }
        });
        panel.add(addButton);

        JButton updateButton = new JButton("查看详情");   //修改按钮
        updateButton.addActionListener(new ActionListener(){//添加事件
            public void actionPerformed(ActionEvent e){
                int selectedRow = table.getSelectedRow();//获得选中行的索引
                if(selectedRow!= -1){   //是否存在选中行
                    int no = (Integer)table.getValueAt(selectedRow, 0);
                    Student stu = Dao.getInstance().queryByNo(no);
                    DetailStudent detail = new DetailStudent(stu);
                    detail.setVisible(true);
                }
            }
        });
        panel.add(updateButton);

        JButton delButton = new JButton("删除");
        delButton.addActionListener(new ActionListener(){//添加事件
            public void actionPerformed(ActionEvent e){
                int selectedRow = table.getSelectedRow();//获得选中行的索引
                if(selectedRow!=-1){  //存在选中行
                    if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(null, "确认要删除吗？","请确认",JOptionPane.YES_NO_OPTION)){
                        int no = (Integer)tableModel.getValueAt(selectedRow, 0);
                        Dao.getInstance().removeRecordByNo(no);
                        tableModel.removeRow(selectedRow);  //删除行
                    }
                }
            }
        });
        panel.add(delButton);
    }

    public JTableStudents(LoginFrame logframe){
        this();
        logframe.dispose();
        setVisible(true);
    }

//    /**
//     * @param args
//     */
//    public static void main(String[] args) {
//        // TODO Auto-generated method stub
//        JTableDefaultTableModelTest jTableDefaultTableModelTest = new JTableDefaultTableModelTest();
//        jTableDefaultTableModelTest.setVisible(true);
//    }

}
